package com.xmler.plugin.adbrocket.utils;

import com.intellij.notification.*;

import java.util.ArrayList;
import java.util.List;

public class Logger {
    private static String NAME;
    private static int LEVEL = 0;

    public static final int DEBUG = 3;
    public static final int INFO = 2;
    public static final int WARN = 1;
    public static final int ERROR = 0;

    private static final List<LogInterceptor> mInterceptor = new ArrayList<>();

    public interface LogInterceptor {
        void log(String message);
    }

    public static void init(String name,int level) {
        NAME = name;
        LEVEL = level;
        NotificationsConfiguration.getNotificationsConfiguration().register(NAME, NotificationDisplayType.NONE);
    }

    public static void addInterceptor(LogInterceptor interceptor) {
        if (!mInterceptor.contains(interceptor)) {
            mInterceptor.add(interceptor);
        }
    }

    private static void log(String message) {
        for (int i = 0; i < mInterceptor.size(); i++) {
            mInterceptor.get(i).log(message);
        }
    }

    public static void d(final String format, final Object... obj) {
        if (LEVEL >= DEBUG) {
            String text = obj == null ? format : String.format(format, obj);
            Notifications.Bus.notify(
                    new Notification(NAME, NAME + " [DEBUG]", text, NotificationType.INFORMATION));
            log(text);
        }
    }



    public static void i(final String format, final Object... obj) {
        if (LEVEL > INFO) {
            String text = obj == null ? format : String.format(format, obj);
            Notifications.Bus.notify(
                    new Notification(NAME, NAME + " [INFO]", text, NotificationType.INFORMATION));
            log(text);
        }
    }

    public static void w(final String format, final Object... obj) {
        if (LEVEL > WARN) {
            String text = obj == null ? format : String.format(format, obj);
            Notifications.Bus.notify(
                    new Notification(NAME, NAME + " [WARN]", text, NotificationType.WARNING));
            log(text);
        }
    }

    public static void e(final String format, final Object... obj) {
        if (LEVEL > ERROR) {
            String text = obj == null ? format : String.format(format, obj);
            Notifications.Bus.notify(
                    new Notification(NAME, NAME + " [ERROR]", text, NotificationType.ERROR));
            log(text);
        }
    }
}
